package com.z.xhelper.core.wx;

import com.z.xhelper.core.config.BeanContainer;
import com.z.xhelper.core.config.Reflections;
import com.z.xhelper.core.utils.LogUtil;
import com.z.xhelper.core.wx.hook.IHook;

import java.util.Arrays;


public class WxService {

    public WxService() {
        // 注册接收微信消息的服务
        registerHook();
        LogUtil.log("注册微信消息服务完成");
    }

    /**
     * 注册接收微信消息的 hook 方法
     * 并将 hook 方法注册到 eventbus 中
     */
    private void registerHook() {
        // 接收微信消息
        // 找到所有 IReceiveMsgHook 的实现类
        Reflections reflections = BeanContainer.getBean(Reflections.class);

        reflections.getSubTypesOf(IHook.class).forEach(clazz -> {
            try {
                IHook receiveMsg = (IHook) clazz.newInstance();
                // 注册到 BeanContainer 中
                BeanContainer.addBean(clazz, receiveMsg);
                // 挂载 hook
                receiveMsg.hook();
            } catch (InstantiationException | IllegalAccessException e) {
                LogUtil.log("注册接收微信消息的hook方法失败"
                        + Arrays.toString(e.getStackTrace()));
            }
        });

        LogUtil.log("注册接收微信消息的方法完成");
    }
}
